<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>企业管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
</head>
<body>
<div style="padding: 10px;">
    <from class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label" style="width: auto;">劳务公司名称</label>
                <div class="layui-input-inline" style="width: 130px;">
                    <input type="text" id="name" name="name" autocomplete="off" class="layui-input">
                </div>
            </div>

            <button class="layui-btn layui-btn-normal" data-type="search" id="btnSearch">查询</button>
            <button class="layui-btn layui-btn-primary" data-type="doAdd" id="btnAdd">添加</button>
            <!--<button class="layui-btn" data-type="doImport" id="btnImport" >导入</button>-->
        </div>
    </from>
</div>
<table class="layui-table" lay-filter="tbFliter" id="tbData" style="margin-top: -25px;">
</table>
<script type="text/html" id="barList">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="view">查看</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="resetpwd">密码重置</a>
</script>
<script src="../assets/layui/layui.js"></script>
<script>
    layui.config({
        base: '../js/'
    });
    layui.use(['jquery','table','common','laydate'], function () {
        var table = layui.table,
            common=layui.common,
            laydate=layui.laydate,
            $ = layui.jquery,
            active = {
                search: function () {
                    table.reload("tbOptions", {
                        where: {
                            name: $("#name").val()
                        },page:{
                            curr: 1
                        }
                    });
                },
                doAdd: function () {
                    common.layer_show("企业新增",'companyAdd.html?type=add');
                },
                doImport: function () {
                    common.layer_show("企业导入",'companyImport.html',"400","300");
                }
            };
        var op=common.getUrlParam("op");
//        var defaultStartTime = common.getTodayStart();
//        var defaultEndTime = common.getTodayEnd();
//        //日期初始化  第一次查询无法获取到laydate值
//        $("#startTime").val(defaultStartTime);
//        $("#endTime").val(defaultEndTime);
//        laydate.render({
//            elem:'#startTime'
//            ,format:'yyyy-MM-dd HH:mm:ss'
//            ,type: 'datetime'
//            ,value:defaultStartTime
//        })
//        laydate.render({
//            elem:'#endTime'
//            ,format:'yyyy-MM-dd HH:mm:ss'
//            ,type: 'datetime'
//            ,value:defaultEndTime
//        })
        //表格初始化
        table.render({
            elem: '#tbData'
            , skin: 'row'
            , url: 'list'
//            ,data:[
//                 {id:"F2020-0010",name:"企业名称",code:"营业执照号",level:"资质等级",userName:"联系人",phone:"13600000000"}
//            ]
            , page: true
            , cols: [[
                  {field: 'name', title: '企业名称'}
                , {field: 'licNo', title: '营业执照号'}
                , {field: 'userName', title: '联系人'}
                , {field: 'phone', title: '手机号/企业账号'}
//                , {field: 'operation', title: '操作', width: 200,templet: function(d){
//                    if(d.status==0){
//                        return '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">启用</a>';
//                    }else if(d.status==1){
//                     var html='<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="view">查看</a>';
//                        html+='<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>';
//                        html+='<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
//                        html+='<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="resetpwd">密码重置</a>';
//                        return html;
//                    }
//                    return "";
//                }}
//                , {field: 'defPwd', title: '默认密码'}
//                 , {field: 'operation', title: '操作', width: 300, toolbar: '#barList'}
                   , {field: 'operation', title: '操作', width: 400,templet: function(d){
                        var html='<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="view">查看</a>';
                        html+='<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>';
                        html+=' <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="resetpwd">密码重置</a>';
                        if(d.status=='0'){
                            html += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                        }else {
                            html+= '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="qiyong">启用</a>';
                        }
                       return html;
               }}
            ]]
            , limits: [10, 20, 50]
            , where: {
                name: $("#name").val()
            }
            , id: 'tbOptions'
        });

        table.on('tool(tbFliter)', function (obj) {
            var data = obj.data; //获得当前行数据
            var id = data.id;
            var phone=data.phone;
            var layEvent = obj.event; //获得 lay-event
            if (layEvent === 'view') { //编辑
                common.layer_show('企业详情',"companyDetail.html?id="+id);
            }
            else if (layEvent === 'edit') { //编辑
                common.layer_show('企业编辑',"companyAdd.html?type=edit&id="+id);
            } else if (layEvent === 'del'||layEvent === 'qiyong') { //删除
                // if(op!="1"){
                //     common.showError("仅能由管理员进行劳务公司的删除");
                //     return false;
                // }
                var str="删除";
                if (layEvent === 'qiyong'){
                    str='启用';
                }
                layer.confirm('您确定'+str+'该劳务公司吗？', function (index) {
                    $.ajax({
                        type: 'POST',
                        url: 'delete',
                        dataType: 'json',
                        data: {id: id,phone:phone,status:layEvent},
                        success: function (rtn) {
                            if (rtn.code == 0) {
                                common.showMsg('操作成功!');
                                // obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                table.reload("tbOptions", {
                                    where: {
                                        name: $("#name").val()
                                    },page:{
                                        curr: 1
                                    }
                                });
                            }
                            else {
                                common.showError("操作失败:" + rtn.msg);
                            }
                        },
                        error: function () {
                            common.showError("操作失败");
                        }
                    });

                });
            }else if(layEvent==='resetpwd') {
                layer.confirm('您确定重置该用户的密码吗？', function (index) {
                    $.ajax({
                        type: 'POST',
                        url: 'pwdResert',
                        dataType: 'json',
                        data: {phone: phone},
                        success: function (rtn) {
                            if (rtn.code === 0) {
                                common.showInfo("操作成功,已重置为系统默认密码:" + rtn.data);
                            }
                            else {
                                common.showError("操作失败:" + rtn.msg);
                            }
                        },
                        error: function () {
                            common.showError("操作失败");
                        }
                    });
                });
            }
        });


        //按钮事件绑定
        $('.layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>